package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.CategoryMenuMapper;
import com.itheima.pojo.CategoryMenu;
import com.itheima.pojo.SysLog;
import com.itheima.service.CategoryMenuService;
import com.itheima.vo.CategoryMenuVO;
import com.itheima.vo.SysLogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class CategoryMenuServiceImpl implements CategoryMenuService {

    @Autowired
    private CategoryMenuMapper categoryMenuMapper;

    /**
     * 添加菜单
     * @param categoryMenu
     */
    @Override
    public void add(CategoryMenu categoryMenu) {
        categoryMenuMapper.insertSelective(categoryMenu);
    }

    /**
     * 删除菜单
     * @param categoryMenu
     */
    @Override
    public void delete(CategoryMenu categoryMenu) {
        categoryMenuMapper.delete(categoryMenu);
    }

    /**
     * 编辑菜单
     * @param categoryMenu
     */
    @Override
    public void edit(CategoryMenu categoryMenu) {
        categoryMenuMapper.updateByPrimaryKey(categoryMenu);
    }

    /**
     * 获取所有菜单列表
     * @return
     */
    @Override
    public List<CategoryMenu> getAllList() {
        return categoryMenuMapper.selectAll();
    }

    /**
     * 获取所有二级菜单-按钮列表
     * @return
     */
    @Override
    public List<CategoryMenu> getButtonAll() {
        Example example = new Example(CategoryMenu.class);
        Example.Criteria criteria = example.createCriteria();
        //添加条件查询
        criteria.andEqualTo("menuLevel",2);
        criteria.andEqualTo("menuType",1);

        //根据权重排序
        example.orderBy("sort").desc();
        List<CategoryMenu> categoryMenus = categoryMenuMapper.selectByExample(example);
        return categoryMenus;
    }

    @Override
    public Page<CategoryMenu> findList(CategoryMenuVO categoryMenuVO) {
        if (categoryMenuVO!=null){
            //设置默认当前页
            if (categoryMenuVO.getCurrentPage() == null || "".equals(categoryMenuVO.getCurrentPage())) {
                categoryMenuVO.setCurrentPage(1);
            }
            //设置默认每页条数
            if (categoryMenuVO.getPageSize() == null || "".equals(categoryMenuVO.getPageSize())) {
                categoryMenuVO.setPageSize(10);
            }
        }else {
            categoryMenuVO = new CategoryMenuVO();
            categoryMenuVO.setCurrentPage(1);
            categoryMenuVO.setPageSize(10);
        }
        Integer currentPage = categoryMenuVO.getCurrentPage();
        Integer pageSize = categoryMenuVO.getPageSize();
        PageHelper.startPage(currentPage,pageSize);
        Example example = createExample(categoryMenuVO);
        return (Page<CategoryMenu>) categoryMenuMapper.selectByExample(example);
    }
    /**
     * 置顶菜单
     * @param uid
     */
    @Override
    public void stick(String uid) {
        int sortMax = categoryMenuMapper.selectSortMax() + 1;
        categoryMenuMapper.updateSortMax(uid,sortMax);
    }

    private Example createExample(CategoryMenuVO categoryMenuVO) {
        Example example=new Example(CategoryMenu.class);
        Example.Criteria criteria = example.createCriteria();
        if(categoryMenuVO!=null) {
            if (categoryMenuVO.getIcon() != null && !"".equals(categoryMenuVO.getIcon())) {
                criteria.andEqualTo("icon", categoryMenuVO.getIcon());
            }
            if (categoryMenuVO.getIsShow() != null && !"".equals(categoryMenuVO.getIsShow())) {
                criteria.andEqualTo("isShow", categoryMenuVO.getIsShow());
            }
            if (categoryMenuVO.getMenuLevel() != null && !"".equals(categoryMenuVO.getMenuLevel())) {
                criteria.andEqualTo("menuLevel", categoryMenuVO.getMenuLevel());
            }
            if (categoryMenuVO.getMenuType() != null && !"".equals(categoryMenuVO.getMenuType())) {
                criteria.andEqualTo("menuType", categoryMenuVO.getMenuType());
            }
            if (categoryMenuVO.getStatus() != null && !"".equals(categoryMenuVO.getStatus())) {
                criteria.andEqualTo("status", categoryMenuVO.getStatus());
            }
            if (categoryMenuVO.getName() != null && !"".equals(categoryMenuVO.getName())) {
                criteria.andEqualTo("name", categoryMenuVO.getName());
            }
            if (categoryMenuVO.getParentUid() != null && !"".equals(categoryMenuVO.getParentUid())) {
                criteria.andEqualTo("parentUid", categoryMenuVO.getParentUid());
            }
            if (categoryMenuVO.getSort() != null && !"".equals(categoryMenuVO.getSort())) {
                criteria.andEqualTo("sort", categoryMenuVO.getSort());
            }
            if (categoryMenuVO.getUid() != null && !"".equals(categoryMenuVO.getUid())) {
                criteria.andEqualTo("uid", categoryMenuVO.getUid());
            }
            if (categoryMenuVO.getSummary() != null && !"".equals(categoryMenuVO.getSummary())) {
                criteria.andEqualTo("summary", categoryMenuVO.getSummary());
            }
            if (categoryMenuVO.getUrl() != null && !"".equals(categoryMenuVO.getUrl())) {
                criteria.andEqualTo("url", categoryMenuVO.getUrl());
            }
        }
        return example;
    }

}
